namespace WebCalendar.Migrations
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Diagnostics;
    using System.Linq;
    using WebCalendar.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<WebCalendar.Models.WebCalendarContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            
        }

        protected override void Seed(WebCalendar.Models.WebCalendarContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
            context.Database.ExecuteSqlCommand("delete from EntryFilters");
            context.Database.ExecuteSqlCommand("delete from PersonalEntries");
            context.Database.ExecuteSqlCommand("delete from Entries");
            context.Database.ExecuteSqlCommand("delete from Roles");
            context.Database.ExecuteSqlCommand("delete from AppointmentRequests");
            context.Database.ExecuteSqlCommand("delete from Calendars");
            context.Database.ExecuteSqlCommand("delete from Categories");
            
            
            Entry e1 = new Entry { Title = "See the doctor", Description = "This is the description of the first entry", StartDate = DateTime.Now, EndDate = DateTime.Now + TimeSpan.FromHours(2)};
            Entry e2 = new Entry { Title = "Football training", Description = "This is the description of the second entry", StartDate = DateTime.Now + TimeSpan.FromHours(28), EndDate = DateTime.Now + TimeSpan.FromHours(31) };
            Entry e3 = new Entry { Title = "Appointment with John", Description = "This is the description of the third entry", StartDate = DateTime.Now + TimeSpan.FromHours(5), EndDate = DateTime.Now + TimeSpan.FromHours(6) };
            Entry e4 = new Entry { Title = "Do the shopping", Description = "This is the description of the fourth entry", StartDate = DateTime.Now + TimeSpan.FromHours(14), EndDate = DateTime.Now + TimeSpan.FromHours(17) };

            context.Entries.AddOrUpdate(e => e.ID, e1, e2, e3, e4);

            UserProfile testA = context.UserProfiles.First(x => x.UserName == "testa");
            UserProfile testB = context.UserProfiles.First(x => x.UserName == "testb");

            if (testA.CalendarRoles == null) testA.CalendarRoles = new List<Role>();
            if (testB.CalendarRoles == null) testB.CalendarRoles = new List<Role>();
            
            Category cA1 = new Category { Name = "BME", Color = "#9A81B5", Entries = new List<PersonalEntry>(), User = testA };
            Category cA2 = new Category { Name = "Work", Color = "#ADCB52", Entries = new List<PersonalEntry>(), User = testA };
            Category cB1 = new Category { Name = "Family", Color = "#F9C33F", Entries = new List<PersonalEntry>(), User = testB };
            Category cB2 = new Category { Name = "Friends", Color = "#66943F", Entries = new List<PersonalEntry>(), User = testB };
            Category cAD = new Category { Name = "unassigned", Color = "#FFFFFF", Entries = new List<PersonalEntry>(), User = testA };
            Category cBD = new Category { Name = "unassigned", Color = "#FFFFFF", Entries = new List<PersonalEntry>(), User = testB };
            
            PersonalEntry pB1 = new PersonalEntry { EntryBase=e2, Category=cB1};
            cB1.Entries.Add(pB1);

            PersonalEntry pB2 = new PersonalEntry() { EntryBase = e4, Category = cB2};
            cB2.Entries.Add(pB2);

            PersonalEntry pA1 = new PersonalEntry() { EntryBase = e1, Category = cA1};
            cA1.Entries.Add(pA1);
            PersonalEntry pA2 = new PersonalEntry() { EntryBase = e2, Category = cA2};
            cA2.Entries.Add(pA2);
            PersonalEntry pA3 = new PersonalEntry() { EntryBase = e3, Category = cA1};
            cA1.Entries.Add(pA3);

            context.PersonalEntries.AddOrUpdate(e => e.ID, pA1, pA2, pA3, pB1, pB2);
            context.Categories.AddOrUpdate(c => c.ID, cAD, cBD);

            Calendar calA1 = new Calendar{ID = 1, Name = "Sport activities", IsPublic = true};
            Calendar calA2 = new Calendar{ID = 2, Name = "Study and Work"};
            Calendar calB1 = new Calendar{ID = 3, Name = "My primary calendar"};

            OwnerRole oA1 = new OwnerRole {User=testA,Calendar=calA1};
            OwnerRole oA2 = new OwnerRole { User = testA, Calendar = calA2 };
            OwnerRole oB1 = new OwnerRole { User = testB, Calendar = calB1 };
            MemberRole oB2 = new MemberRole { User = testB, Calendar = calA2 };

            context.CalendarRoles.AddOrUpdate(r => r.ID, oA1, oA2, oB1, oB2);

            calA1.CalendarRoles.Add(oA1);
            calA2.CalendarRoles.Add(oA2);
            calA2.CalendarRoles.Add(oB2);
            calB1.CalendarRoles.Add(oB1);

            testA.CalendarRoles.Add(oA1);
            testA.CalendarRoles.Add(oA2);
            testB.CalendarRoles.Add(oB1);
            testB.CalendarRoles.Add(oB2);

            EntryFilter ef1 = new EntryFilter { Entry = pA1, Visible = true };
            EntryFilter ef2 = new EntryFilter {Entry = pA2, Visible = true};
            EntryFilter ef3 = new EntryFilter {Entry = pA3, Visible = true};
            EntryFilter ef4 = new EntryFilter {Entry = pB1, Visible = true};
            EntryFilter ef5 = new EntryFilter {Entry = pB2, Visible = true};

            calA2.EntryFilters.Add(ef4);
            calA2.EntryFilters.Add(ef2);
            calA1.EntryFilters.Add(ef1);
            calA1.EntryFilters.Add(ef3);
            calB1.EntryFilters.Add(ef5);
                        
            context.Calendars.AddOrUpdate(c => c.ID, calA1, calA2, calB1);
            context.EntryFilters.AddOrUpdate(f => f.ID, ef1, ef2, ef3, ef4, ef5);
            context.SaveChanges();
        }
    }
}
